import api from "@/api/login.js";

export async function setLoginInfo(data) {
  localStorage.setItem("expiresTime", data.expiresTime);
  localStorage.setItem("accessToken", data.accessToken);
  localStorage.setItem("userId", data.userId);
  localStorage.setItem("refreshToken", data.refreshToken);
  localStorage.setItem("loginInfo", JSON.stringify(data));
  try {
    const res = await api.getUserInfo();
    if (res.code === 0) {
      localStorage.setItem("userInfo", JSON.stringify(res.data.user));
    }
  } catch (error) {
    console.log(error);
  }
  return true;
}

export async function removeLoginInfo() {
  localStorage.removeItem("expiresTime");
  localStorage.removeItem("accessToken");
  localStorage.removeItem("userId");
  localStorage.removeItem("userInfo");
  localStorage.removeItem("refreshToken");
  localStorage.removeItem("loginInfo");
  return true;
}
